package com.mys.action;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mys.model.IpPojo;
import com.mys.service.GetData;
import com.mys.service.Pagination;


@Controller
@RequestMapping("/getip")
public class GetIpController {
	
	
	private int page = 1;	
	private Pagination pagination;
	
	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}


	@Resource(name = "getData")
	private GetData ipGetData;
	

	
	@RequestMapping(value="/getallip")
	@ResponseBody
	public Map<String, Object> getAllIP(@RequestParam("page") int page,@RequestParam("rows") int rows){
		
		Map<String, Object> result = new HashMap<String, Object>(2);
		List ipbean = ipGetData.getResultByPage(page, rows);
		
		
		
		//int totalResult = ipGetData.getTotalPages();
		int totalResult = ipGetData.getAll().size();
		//System.out.print(totalResult);
		result.put("total", totalResult);
		result.put("rows", ipbean);
	    //result.remove("page");
	    //result.remove("totalPages");
	   
		return result;
	}
	
	
	@RequestMapping(value="/delid/{id}",method = RequestMethod.GET)
	public String delId(@PathVariable String id,HttpServletRequest request,HttpServletResponse response){
		ipGetData.delId(id);
		return "table";
	}
	
	
	
	@RequestMapping(value="/getjsonbyid/{id}",method = RequestMethod.GET)
	public @ResponseBody ModelMap getipbyip(@PathVariable String id,
			HttpServletRequest request,   
            HttpServletResponse response){
		IpPojo getdata = ipGetData.findById(id);
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("ipbean", getdata);
		return modelMap;
		
	}
}
